﻿using System.Web.Security;
using System.Web;
using Super.Common.Strings;

namespace Super.Common.Security
{
    public class MD5
    {

        protected static string SystemKey = "bangq_of_super"; //系统盐，用于存储数据库,一旦定义就不能更改(发布后)
        protected static string PublicKey = "#$^$%YT%EGG#@QGW$"; //公共key，用于暴露的加密盐

        /// <summary>
        /// MD5穿插算法
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        /// <remarks>Richwong 11-07-13</remarks>
        private static string BangKeEncrypt(string str)
        {
            string SourceSecurity = GetMD5(str); //原MD5值
            string BangKeSecurity = SourceSecurity.Substring(3, 13); //取原值的从3开始13个字符
            string BangkeSecurityKey = GetMD5(BangKeSecurity).Substring(21,8);
            return SourceSecurity.Substring(19, 11)+BangkeSecurityKey+BangKeSecurity;  //取原值的11位+8key+13位原截

        }

        /// <summary>
        /// 公共加盐MD5函数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ADD(string str)
        {
            return BangKeEncrypt(str+PublicKey);
        }
        
        /// <summary>
        /// 系统加盐MD5函数,用于数据存储,如用户,admin到数据表
        /// </summary>
        /// <param name="content">传入的明文</param>
        /// <returns>返回MD5系统穿插密文，32位大写</returns>
        public static string GET(string content)
        {
            return BangKeEncrypt(content + SystemKey);
        }
        /// <summary>
        /// 管理员的Cookie加密算法
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string FormatCookieByAdmin(string name)
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            string agent = HttpContext.Current.Request.UserAgent;
            var platForm = HttpContext.Current.Request.Browser.Platform; //客户端系统平台
            var browserVer = HttpContext.Current.Request.Browser.Version; //客户端浏览器版本号
            var browserId = HttpContext.Current.Request.Browser.Id; //客户端浏览器标识
            name = HttpContext.Current.Server.UrlDecode(name);
            string c_key = GetMD5(browserId + browserVer + ip + agent + PublicKey + name + platForm);
            return c_key;
        }
        /// <summary>
        /// 用户的Cookie加密算法
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string FormatCookieByUser(string name)
        {
            //承志: 对于多内核浏览器有问题.
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            //string agent = HttpContext.Current.Request.UserAgent;
            var platForm = HttpContext.Current.Request.Browser.Platform; //客户端系统平台
            //var browserVer = HttpContext.Current.Request.Browser.Version; //客户端浏览器版本号
            //var browserId = HttpContext.Current.Request.Browser.Id; //客户端浏览器标识
            name = HttpContext.Current.Server.UrlDecode(name);
            string ckey = GetMD5(ip + PublicKey + name + platForm);// GetMD5(browserId + browserVer + ip + agent + PublicKey + name + platForm);
            return ckey;
        }

        /// <summary>
        /// 标准MD5算法
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string GetMD5(string content)
        {
            lock (typeof(MD5))
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(content, "MD5");
            }
        }

        public static byte[] GetMD5(byte[] content)
        {
            lock (typeof(MD5))
            {
                return StringCoder.StringToByte(GetMD5(StringCoder.ByteToString(content)));
            }
        }

        public static bool Compare(string contentA, string contentB)
        {
            if (GetMD5(contentA).Trim().ToUpper() == GetMD5(contentB).ToUpper().Trim())
                return true;
            return false;
        }

        public static bool Compare(byte[] contentA, byte[] contentB)
        {
            return Compare(StringCoder.ByteToString(contentA), StringCoder.ByteToString(contentB));
        }

    }

}
